<template>
  <div class="app-container">
    <el-form :model="queryForm" ref="queryFormRef" :inline="true">
      <el-form-item prop="status" label="状态">
        <el-select v-model="queryForm.status" placeholder="请选择系统操作日志状态">
          <el-option :value="2">正常</el-option>
          <el-option :value="1">关闭</el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="createdAt" label="创建时间">
        <el-date-picker disabled v-model="queryForm.createdAt" style="width: 254px" />
      </el-form-item>
      <el-form-item>
        <el-space>
          <el-button type="primary" @click="handleQuery" icon="Search"> 搜索</el-button>
          <el-button @click="handleResetQuery" icon="Refresh"> 重置</el-button>
        </el-space>
      </el-form-item>
    </el-form>

    <el-divider />

    <div class="action">
      <el-space>
        <el-button type="primary" status="danger" @click="handleBatchDelete" :disabled="selectedKeys.length === 0"
          icon="Delete"> 批量删除</el-button>
      </el-space>
    </div>

    <el-table :data="tableData" row-key="id">
      <el-table-column label='编号' prop='id'></el-table-column>
      <el-table-column label='请求信息' prop='operUrl'></el-table-column>
      <el-table-column label='操作人员' prop='operName'></el-table-column>
      <el-table-column label='状态' prop='status'>
        <template #default="scope">
          <el-tag v-if="scope.row.status == 2" type="success">正常</el-tag>
          <el-tag v-if="scope.row.status == 1" type="danger">关闭</el-tag>
        </template>
      </el-table-column>
      <el-table-column label='操作日期' prop='operTime'>
        <template #default="scope">
          {{ parseTime(scope.row.operTime) }}
        </template>
      </el-table-column>
      <el-table-column label='操作' prop='action'>
        <template #default="scope">
          <el-popconfirm content="是否删除当前数据？" type="warning" @ok="removeSysOperaLogInfo([scope.row.id])">
            <el-button text type="primary" status="danger">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>

    </el-table>

    <el-pagination :page-size="10" v-model="currentPage"  layout="prev, pager, next" :total="pager.count"
      @current-change="handlePageChange" />


  </div>
</template>

<script setup>
import { ref, reactive, onMounted, getCurrentInstance } from 'vue';
import { getSysOperaLog, removeSysOperaLog } from '@/api/admin/sys-opera-log';
import { parseTime } from '@/utils/parseTime';

const { proxy } = getCurrentInstance();

// 当前页
const currentPage = ref(1);

// 分页
const pager = {
  count: 0,
  pageIndex: 1,
  pageSize: 10,
};

//
const selectedKeys = ref([]);
const queryForm = reactive({});
const queryFormRef = ref({});
const tableData = ref([]);

// 查询
const handleQuery = () => {
  handlePageChange(1);
};

// 重置查询
const handleResetQuery = () => {
  queryFormRef.value.resetFields();
  handlePageChange(1);
};

// 批量删除
const handleBatchDelete = () => {
  console.log(selectedKeys.value);
  if (selectedKeys.value.length === 0) {
    proxy.$message.error('请勾选需要删除的数据！');
    return;
  }

  proxy.$modal.confirm({
    title: '注意',
    content: '是否批量删除选中？',
    onOk: () => {
      removeSysOperaLogInfo(selectedKeys.value);
    },
    onCancel: () => {
      proxy.$message.info('已取消操作');
    },
  });
};

// 页码改变
const handlePageChange = (page) => {
  currentPage.value = page;

  pager.pageIndex = page;
  getSysOperaLogInfo({ ...pager, ...queryForm });
};

//
const handlePageSizeChange = (pageSize) => {
  pager.pageSize = pageSize;
  getSysOperaLogInfo({ ...pager, ...queryForm });
};

// 获取操作日志
const getSysOperaLogInfo = async (params = {}) => {
  const res = await getSysOperaLog(params);
  const { count, list, pageIndex, pageSize } = res.data;
  tableData.value = list;

  Object.assign(pager, { total: count, pageIndex, pageSize });
};

/**
 * 删除操作日志
 * @params {array} ids
 */
const removeSysOperaLogInfo = async (ids) => {
  const res = await removeSysOperaLog({ ids });
  proxy.$message.success(res.msg);

  getSysOperaLogInfo();
};

onMounted(async () => {
  getSysOperaLogInfo();
});
</script>

<style lang="scss">
.action {
  margin-bottom: 8px;
}
</style>
